﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Data;

public partial class main_uc_app_det_main : DAO.ESWebUserControl
{
    public string _lang = "";
    public string application_id = "0";
    public bool edit_app = false;
    public string type_transs_id = "";
    public string type_offs_id = "";
    string referrer_addr = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        InitializeUI();
        if (!Page.IsPostBack)
        {
            LoadCountry();
            LoadTime();
            LoadLevelOfImportanceTypes(this.LangId);
            LoadPlacementTypes(this.LangId);
            LoadPackingTypes(this.LangId);
            LoadSpecialCondTypes(this.LangId);
            LoadDimensionTypes(this.LangId);
            LoadVolumeTypes(this.LangId);
            LoadCurrencyTypes(this.LangId);
            LoadWeightTypes(this.LangId);
            LoadAppDetails();
        }
    }

    #region LoadCountry
    private void LoadCountry()
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT id, short, name FROM dbo.Fn_GetAllPlaces(0, 1000000, 'id', 'asc', 2) WHERE isActive='true'", conn))
            {
                SqlDataReader dr = cmd.ExecuteReader();
                ddlCountryLoading.Items.Add(new ListItem("", "0"));
                ddlCountryUnloading.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    ddlCountryLoading.Items.Add(new ListItem(dr["name"].ToString() + " - " + dr["short"].ToString(), dr["id"].ToString()));
                    ddlCountryUnloading.Items.Add(new ListItem(dr["name"].ToString() + " - " + dr["short"].ToString(), dr["id"].ToString()));
                }
                dr.Close();
            }
        }
    }
    #endregion

    #region LoadTime
    private void LoadTime()
    {
        tbTimeLoading.Items.Clear();
        tbTimeUnloading.Items.Clear();
        tbTimeLoading.Items.Add(new ListItem("", "0"));
        tbTimeUnloading.Items.Add(new ListItem("", "0"));
        for (int i = 12; i > 0; i--)
        {
            if (i < 10)
            {
                tbTimeLoading.Items.Add(new ListItem("0" + i.ToString() + (":00"), i.ToString() + ":00"));
                tbTimeUnloading.Items.Add(new ListItem("0" + i.ToString() + ":00", i.ToString() + ":00"));
            }
            else
            {
                tbTimeLoading.Items.Add(new ListItem(i.ToString() + (":00"), i.ToString() + ":00"));
                tbTimeUnloading.Items.Add(new ListItem(i.ToString() + ":00", i.ToString() + ":00"));
            }
        }
    }
    #endregion

    #region LoadAppDetails
    private void LoadAppDetails()
    {
        if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
        {
            using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT * FROM Fn_ApplicationById(@id)", conn))
                {
                    int appid = 0;
                    application_id = Request.QueryString["id"].ToString();
                    Int32.TryParse(Request.QueryString["id"].ToString(), out appid);
                    cmd.Parameters.Add("id", appid);
                    SqlDataReader dr = cmd.ExecuteReader();

                    if (dr.Read())
                    {
                        tb_appid.Text = Request.QueryString["id"].ToString();
                        tb_type_transport_id.Text = dr["type_transport_id"].ToString();
                        tb_type_offer_id.Text = dr["type_offer_id"].ToString();
                        if (dr["isActive"]!=null)
                        cb_isActive.Checked = (dr["isActive"].ToString() == "True") ? true : false;

                        ShowHideEditFields(false);//Change this to true
                        //Edit Application
                        if (Request.Cookies["texchange"] != null && Request.Cookies["texchange"]["logged"] != null && DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["logged"]) == "true")
                        {
                            btn_Save_to_Favorites.Visible = true;
                            if (Request.Cookies["texchange"]["cid"] != null && DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["cid"].ToString()) == dr["company_id"].ToString())
                            { 
                                if (Request.UrlReferrer!=null)
                                {
                                    referrer_addr = Request.UrlReferrer.ToString();
                                    if (referrer_addr!=null && referrer_addr != "")
                                    {
                                        if (referrer_addr.ToLower().IndexOf("myapplications.aspx") != -1)
                                        {
                                            edit_app = true;
                                            ShowHideEditFields(true);
                                            btn_Update_App.Visible = true;
                                            btn_UpdateTechicalInfo.Visible = true;
                                            btn_add_aditional.Visible = true;
                                            btn_Delete_App.Visible = true;
                                        }
                                    }
                                }
                            }
                            if (Request.Cookies["texchange"]["type"] != null && DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["type"].ToString()) == "admin")
                            {
                                edit_app = true;
                                ShowHideEditFields(true);
                                btn_Update_App.Visible = true;
                                btn_UpdateTechicalInfo.Visible = true;
                                btn_add_aditional.Visible = true;
                                lblIsActive.Visible = true;
                                cb_isActive.Visible = true;
                                btn_Delete_App.Visible = true;
                            }
                            Dictionary<long, long> vehicle_types = new Dictionary<long, long>();
                            vehicle_types[166] = 13;
                            vehicle_types[164] = 14;
                            vehicle_types[165] = 3;
                            vehicle_types[167] = 1;
                            long type_trans_id = 0;
                            if (Int64.TryParse(dr["type_transport_id"].ToString(), out type_trans_id))
                            {
                                LoadVehicleTypes(this.LangId, vehicle_types[type_trans_id]);
                            }
                        }

                        long type_offer_id = Int64.Parse(dr["type_offer_id"].ToString());
                        long type_transport_id = Int64.Parse(dr["type_transport_id"].ToString());
                        type_offs_id = dr["type_offer_id"].ToString();
                        type_transs_id = dr["type_transport_id"].ToString();

                        //Load Loading-Unloading Info
                        LoadLoadingUnloadingInfo(appid, type_transport_id);

                        //Load Tecnical Informations
                        if (type_offs_id == "71")
                        {
                            Control tender_app = LoadControl("uc_tender_app.ascx");
                            ph_TenderApp.Controls.Add(tender_app);
                            ph_TechInfo.Visible = false;
                        }
                        else
                        {
                            switch (type_transs_id)
                            {
                                case "164"://Road
                                case "166"://Rail
                                case "167"://Air
                                    LoadTecnicalInfo(appid, type_transport_id, type_offer_id);
                                break;
                                case "165"://Sea
                                    if (type_offs_id == "69")//Cargo
                                    {
                                        LoadTecnicalInfo(appid, type_transport_id, type_offer_id);
                                    }
                                    else if (type_offs_id == "70")//Transport
                                    {
                                        Control tender_app = LoadControl("uc_sea_transport_app.ascx");
                                        ph_TenderApp.Controls.Add(tender_app);
                                        ph_TechInfo.Visible = false;
                                        //TO DO 
                                    }
                                break;
                                
                            }
                        }

                        //Company Informations
                        lblCompName.Text = dr["company_name"].ToString() != "" ? dr["company_name"].ToString() : "N/A";
                        lblCompanyName.Text = dr["company_name"].ToString() != "" ? dr["company_name"].ToString() : "N/A";
                        if (dr["member_type_id"]!=null)
                        {
                            switch (dr["member_type_id"].ToString())
                            {
                                case "1":
                                     lblCompanyStatus.Text = "*** "+DAO.TextBaseFactory.loadText("gold_member",this.LangId);
                                    break;
                                case "2":
                                    lblCompanyStatus.Text = "** " + DAO.TextBaseFactory.loadText("silver_member", this.LangId);
                                    break;
                                case "3":
                                    lblCompanyStatus.Text = "* " + DAO.TextBaseFactory.loadText("bronze_member", this.LangId);
                                    break;
                                default:
                                    lblCompanyStatus.Text = "N/A"; 
                                   break;
                            }
                        }
                        
                        long country_id = 0;
                        lblCountry.Text = "N/A";
                        if (Int64.TryParse(dr["country_id"].ToString(), out country_id))
                        {
                            string comp_country = LoadCountryById(country_id, false);
                            string short_code = LoadCountryById(country_id, true);
                            lblCountry.Text = "<img width=\"20\" title='" + comp_country +
                            "' height=\"12\" src=\"images/countries/" + short_code + "-t.gif\" /> - "+
                             short_code + " " + comp_country; 
                            
                        }
                        lblCity.Text = dr["City"].ToString() != "" ? dr["City"].ToString() : "N/A";
                        lblPostCode.Text = dr["postcode"].ToString() != "" ? dr["postcode"].ToString() : "N/A";
                        lblAddress.Text = dr["address"].ToString() != "" ? dr["address"].ToString() : "N/A";
                        lblCompanyType.Text = dr["company_type"].ToString() != "" ? dr["company_type"].ToString() : "N/A";
                        lblOperatingLicence.Text = dr["operating_licence"].ToString() != "" ? dr["operating_licence"].ToString() : "N/A";
                        lblOperatingActivity.Text = dr["operating_activity"].ToString() != "" ? dr["operating_activity"].ToString() : "N/A";
                        using (SqlCommand cmdCompT = new SqlCommand("SP_TransportTypesByCompany", conn))
                        {
                            cmdCompT.CommandType = System.Data.CommandType.StoredProcedure;
                            cmdCompT.Parameters.Add("id", dr["company_id"].ToString());
                            cmdCompT.Parameters.Add("language_id", this.LangId);
                            SqlDataReader drcompT = cmdCompT.ExecuteReader();
                            if (drcompT.HasRows)
                            {
                                while (drcompT.Read())
                                {
                                    lblTransportType.Text += drcompT["name"] + "<br>";
                                }
                            }
                            else
                            {
                                lblTransportType.Text = "N/A";
                            }

                            drcompT.Close();
                        }
                        //Contact details
                        lblTelephone.Text = dr["telephone"].ToString() != "" ? dr["telephone"].ToString() : "N/A";
                        lblFax.Text = dr["fax"].ToString() != "" ? dr["fax"].ToString() : "N/A";
                        lblMobile.Text = dr["mobile"].ToString() != "" ? dr["mobile"].ToString() : "N/A";
                        lblWebsite.Text = dr["website"].ToString() != "" ? dr["website"].ToString() : "N/A";
                        lblContactPerson.Text = dr["contact_person"].ToString() != "" ? dr["contact_person"].ToString() : "N/A";
                        lblJobTitle.Text = dr["job_title"].ToString() != "" ? dr["job_title"].ToString() : "N/A";
                        if (dr["fileimage"].ToString() != "")
                        {
                            string path = dr["company_id"].ToString() + "_" + dr["fileimage"].ToString();
                            if (File.Exists(Server.MapPath("~/images/company/" + path)))
                                imgLogo.ImageUrl = "~/images/company/" + path;
                        }
                        if (dr["language"] != null)
                        {
                            string[] languages = dr["language"].ToString().Split(',');
                            string langs = "";
                            for (int i = 0; i < languages.Length; i++)
                            {
                                long lang_id = 0;
                                if (Int64.TryParse(languages[i], out lang_id))
                                {
                                    langs += LoadCountryById(lang_id, true) + " - " + LoadCountryById(lang_id, false)+"<br/>";
                                }
                            }
                            if (langs.Length>2)
                            lblLanguages.Text = langs.ToUpper().Substring(0, langs.Length - 2);
                        }
                    }
                    dr.Close();
                }
            }
        }
    }
    #endregion

    #region ShowHideEditFields
    private void ShowHideEditFields(bool p)
    {
        if (p)
        {
            //Loading Unloading fields
            tbLoadingID.Visible = true;
            tbUnloadingID.Visible = true;
            tbCityLoading.Visible = true;
            tbCityUnloading.Visible = true;
            tbDateLoading.Visible = true;
            tbDateUnloading.Visible = true;
            tbPostCodeLoading.Visible = true;
            tbPostCodeUnloading.Visible = true;
            tbTimeLoading.Visible = true;
            tbTimeUnloading.Visible = true;
            ddlCountryLoading.Visible = true;
            ddlCountryUnloading.Visible = true;
            ddlAMPMLoading.Visible = true;
            ddlAMPMUnloading.Visible = true;
            //Loading Unloading labels
            lbl_loadingID.Visible = true;
            lbl_unloadingID.Visible = true;
            lblLoadingCity.Visible = false;
            lblLoadingCountry.Visible = false;
            lblLoadingDate.Visible = false;
            lblLoadingPostCode.Visible = false;
            lblLoadingTime.Visible = false;
            lblUnLoadingCity.Visible = false;
            lblUnLoadingCountry.Visible = false;
            lblUnLoadingDate.Visible = false;
            lblUnLoadingPostCode.Visible = false;
            lblUnLoadingTime.Visible = false;
            //techical information fields
            ddlLevelOfImportnce.Visible = true;
            ddlVehicleTypes.Visible = true;
            ddlPlacementTypes.Visible = true;
            ddlPackingTypes.Visible = true;
            ddlSpecualConditionTypes.Visible = true;
            ddlPrice.Visible = true;
            ddlVolume.Visible = true;
            ddlWidth.Visible = true;
            ddlHeight.Visible = true;
            ddlWeight.Visible = true;
            ddlHeight.Visible = true;
            ddlLenght.Visible = true;
            tbPrice.Visible = true;
            tbVolume.Visible = true;
            tbWidth.Visible = true;
            tbLenght.Visible = true;
            tbWeight.Visible = true;
            tbHeight.Visible = true;
            tbOtherInformattions.Visible = true;
            //techical information labels
            lblLevelOfImportance.Visible = false;
            lblVehicleType.Visible = false;
            lblPlacementType.Visible = false;
            lblPackingType.Visible = false;
            lblSpecCondition.Visible = false;
            lblPrice.Visible = false;
            lblVolume.Visible = false;
            lblWidth.Visible = false;
            lblWeight.Visible = false;
            lblHeight.Visible = false;
            lblLenght.Visible = false;
            lblOtherInfo.Visible = false;
            
        }
        else
        {
            //Loading Unloading fields
            tbLoadingID.Visible = false;
            tbUnloadingID.Visible = false;
            tbCityLoading.Visible = false;
            tbCityUnloading.Visible = false;
            tbDateLoading.Visible = false;
            tbDateUnloading.Visible = false;
            tbPostCodeLoading.Visible = false;
            tbPostCodeUnloading.Visible = false;
            tbTimeLoading.Visible = false;
            tbTimeUnloading.Visible = false;
            ddlCountryLoading.Visible = false;
            ddlCountryUnloading.Visible = false;
            ddlAMPMLoading.Visible = false;
            ddlAMPMUnloading.Visible = false;
            //Loading Unloading labels
            lbl_loadingID.Visible = false;
            lbl_unloadingID.Visible = false;
            lblLoadingCity.Visible = true;
            lblLoadingCountry.Visible = true;
            lblLoadingDate.Visible = true;
            lblLoadingPostCode.Visible = true;
            lblLoadingTime.Visible = true;
            lblUnLoadingCity.Visible = true;
            lblUnLoadingCountry.Visible = true;
            lblUnLoadingDate.Visible = true;
            lblUnLoadingPostCode.Visible = true;
            lblUnLoadingTime.Visible = true;
            //techical information fields
            ddlLevelOfImportnce.Visible = false;
            ddlVehicleTypes.Visible = false;
            ddlPlacementTypes.Visible = false;
            ddlPackingTypes.Visible = false;
            ddlSpecualConditionTypes.Visible = false;
            ddlPrice.Visible = false;
            ddlVolume.Visible = false;
            ddlWidth.Visible = false;
            ddlWeight.Visible = false;
            ddlHeight.Visible = false;
            ddlLenght.Visible = false;
            tbPrice.Visible = false;
            tbVolume.Visible = false;
            tbWidth.Visible = false;
            tbLenght.Visible = false;
            tbHeight.Visible = false;
            tbWeight.Visible = false;
            tbOtherInformattions.Visible = false;
            //techical information labels
            lblLevelOfImportance.Visible = true;
            lblVehicleType.Visible = true;
            lblPlacementType.Visible = true;
            lblPackingType.Visible = true;
            lblSpecCondition.Visible = true;
            lblPrice.Visible = true;
            lblVolume.Visible = true;
            lblWidth.Visible = true;
            lblWeight.Visible = true;
            lblHeight.Visible = true;
            lblLenght.Visible = true;
            lblOtherInfo.Visible = true;            
        }
        //Company Information
        tbExt.Visible = false;
        tbAddress.Visible = false;
        tbCity.Visible = false;
        tbCompName.Visible = false;
        tbOperatingActivity.Visible = false;
        tbOperatingLicence.Visible = false;
        tbPostCode.Visible = false;
        ddlCompanyType.Visible = false;
        ddlCompanyCountry.Visible = false;
        cbTransportType.Visible = false;
        tbTelephone.Visible = false;
        tbFax.Visible = false;
        tbMobile.Visible = false;
        tbWebsite.Visible = false;
        tbContactPerson.Visible = false;
        tbLanguages.Visible = false;
        tbJobTitle.Visible = false;
    }
    #endregion

    #region LoadLoadingUnloadingInfo
    private void LoadLoadingUnloadingInfo(long app_id, long type_transport_id)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Fn_getLoadingUnloadingByAppId(@app_id)", conn))
            {                
                cmd.Parameters.Add("app_id", app_id);
                SqlDataReader dr = cmd.ExecuteReader();
                int count = 0;
                while (dr.Read())
                {
                    count++;
                    if (count == 1)
                    {
                        tbLoadingID.Text = dr["loading_id"].ToString();
                        tbUnloadingID.Text = dr["unloading_id"].ToString();

                        //Loading
                        DateTime date_var;
                        lblLoadingDate.Text = "N/A";
                        if (DateTime.TryParse(dr["loading_date"].ToString(), out date_var))
                        {
                            tbDateLoading.Text = date_var.ToShortDateString();
                            lblLoadingDate.Text = date_var.ToShortDateString();
                        }
                        lblLoadingTime.Text = dr["loading_time"].ToString() != "" ? dr["loading_time"].ToString() : "N/A";
                        if (dr["loading_time"].ToString() != "")
                        {
                            string[] time = dr["loading_time"].ToString().Split(' ');
                            if (time.Length == 2)
                            {
                                if (tbTimeLoading.Items.FindByValue(time[0])!=null)
                                    tbTimeLoading.Items.FindByValue(time[0]).Selected = true;
                                if (ddlAMPMLoading.Items.FindByValue(time[1])!=null)
                                ddlAMPMLoading.Items.FindByValue(time[1]).Selected = true;
                            }
                        }
                        Int64 country_id = 0;
                        lblLoadingCountry.Text = "N/A";
                        if (Int64.TryParse(dr["loading_country_id"].ToString(), out country_id))
                        {
                            string country_loading = LoadCountryById(country_id, false);
                            string country_short = country_loading;
                            if (country_loading.Length > 14)
                            {
                                country_short = country_loading.Substring(0, 14) + "...";
                            }
                            lblLoadingCountry.Text = "<img width=\"20\" title='" + country_loading +
                            "' height=\"12\" src=\"images/countries/" + dr["loading_country_code"].ToString() + "-t.gif\" /> - " +
                            dr["loading_country_code"].ToString() + " " + country_short;
                            ddlCountryLoading.Items.FindByValue(dr["loading_country_id"].ToString()).Selected = true;
                        }
                        lblLoadingCity.Text = dr["loading_city"].ToString() != "" ? dr["loading_city"].ToString() : "N/A";
                        tbCityLoading.Text = lblLoadingCity.Text;
                        lblLoadingPostCode.Text = dr["loading_postcode"].ToString() != "" ? dr["loading_postcode"].ToString() : "N/A";
                        tbPostCodeLoading.Text = lblLoadingPostCode.Text;
                        if (type_transport_id == 167)
                        {
                            if (!edit_app)
                            {
                                lbl_airport_loading.Visible = true;
                            }
                            else
                            {
                                ddlAirportLoading.Visible = true;
                            }
                            lblAirportLoading.Visible = true;
                            lblAirportLoading.Text = dr["air_loading_country_name"].ToString() != ""
                                ? dr["air_loading_country_code"].ToString() + " - " + dr["air_loading_country_name"].ToString() : "N/A";
                            if (dr["air_loading_id"].ToString()=="")
                            {
                                lblAirportLoading.Text = dr["loading_other_airport"].ToString() != "" ? dr["loading_other_airport"].ToString() : "N/A";
                            }
                        }
                        if (type_transport_id == 165)
                        {
                            if (!edit_app)
                            {
                                lblSeaPortLoading.Visible = true;
                            }
                            else
                            {
                                ddlSeaPortLoading.Visible = true;
                            }
                            lbl_seaport_loading.Visible = true;
                            lblSeaPortLoading.Text = dr["sea_loading_country_name"].ToString() != ""
                                ? dr["sea_loading_country_code"].ToString() + " - " + dr["sea_loading_country_name"].ToString() : "N/A";
                            if (dr["sea_loading_id"].ToString() == "")
                            {
                                lblSeaPortLoading.Text = dr["loading_other_seaport"].ToString() != "" ? dr["loading_other_seaport"].ToString() : "N/A";
                            }
                        }
                        
                        //Unloading
                        lblUnLoadingDate.Text = "N/A";
                        if (DateTime.TryParse(dr["unloading_date"].ToString(), out date_var))
                        {
                            lblUnLoadingDate.Text = date_var.ToShortDateString();
                            tbDateUnloading.Text = date_var.ToShortDateString();
                        }
                        lblUnLoadingTime.Text = dr["unloading_time"].ToString() != "" ? dr["unloading_time"].ToString() : "N/A";
                        if (dr["unloading_time"].ToString() != "")
                        {
                            string[] time = dr["unloading_time"].ToString().Split(' ');
                            if (time.Length == 2)
                            {
                                if (tbTimeUnloading.Items.FindByValue(time[0])!=null)
                                    tbTimeUnloading.Items.FindByValue(time[0]).Selected = true;
                                if (ddlAMPMUnloading.Items.FindByValue(time[1]) != null)
                                ddlAMPMUnloading.Items.FindByValue(time[1]).Selected = true;
                            }
                        }
                        lblUnLoadingCountry.Text = "N/A";
                        if (Int64.TryParse(dr["unloading_country_id"].ToString(), out country_id))
                        {
                            //lblUnLoadingCountry.Text = LoadCountryById(country_id, false);
                            string country = LoadCountryById(country_id, false);
                            string country_short = country;
                            if (country.Length > 14)
                            {
                                country_short = country.Substring(0, 14) + "...";
                            }
                            lblUnLoadingCountry.Text = "<img width=\"20\" title='" + country +
                            "' height=\"12\" src=\"images/countries/" + dr["unloading_country_code"].ToString() + "-t.gif\" /> - " +
                            dr["unloading_country_code"].ToString() + " " + country_short;
                            ddlCountryLoading.Items.FindByValue(dr["loading_country_id"].ToString()).Selected = true;
                            ddlCountryUnloading.Items.FindByValue(dr["unloading_country_id"].ToString()).Selected = true;
                        }
                        lblUnLoadingCity.Text = dr["unloading_city"].ToString() != "" ? dr["unloading_city"].ToString() : "N/A";
                        tbCityUnloading.Text = lblUnLoadingCity.Text;
                        lblUnLoadingPostCode.Text = dr["unloading_postcode"].ToString() != "" ? dr["unloading_postcode"].ToString() : "N/A";
                        tbPostCodeUnloading.Text = lblUnLoadingPostCode.Text;
                        
                        if(type_transport_id==167)
                        {
                            lblAirportUnloading.Visible = true;
                            if (!edit_app)
                            {
                                lbl_aiport_unloading.Visible = true;
                            }
                            else
                            {
                                ddlAirportUnloading.Visible = true;
                            }
                           
                            lblAirportUnloading.Text = dr["air_unloading_country_name"].ToString() != ""
                           ? dr["air_unloading_country_code"].ToString() + " - " + dr["air_unloading_country_name"].ToString() : "N/A";
                            if (dr["air_unloading_id"].ToString() == "")
                            {
                                lblAirportUnloading.Text = dr["unloading_other_airport"].ToString() != "" ? dr["unloading_other_airport"].ToString() : "N/A";
                            }
                        }
                        if (type_transport_id == 165)
                        {
                            lbl_seaport_unloading.Visible = true;
                            if (!edit_app)
                            {
                                lblSeaPortUnloading.Visible = true;
                            }
                            else
                            {
                                ddlSeaPortUnloading.Visible = true;
                            }
                            lblSeaPortUnloading.Text = dr["sea_unloading_country_name"].ToString() != ""
                                ? dr["sea_unloading_country_code"].ToString() + " - " + dr["sea_unloading_country_name"].ToString() : "N/A";
                            if (dr["sea_unloading_id"].ToString() == "")
                            {
                                lblSeaPortUnloading.Text = dr["unloading_other_seaport"].ToString() != "" ? dr["unloading_other_seaport"].ToString() : "N/A";
                            }
                        }
                    }

                    if (count > 1 || edit_app)
                    {
                        StringBuilder loading_unloading = new StringBuilder();
                        //Loading
                        loading_unloading.Append("<div class='destination'><b>" 
                            + DAO.TextBaseFactory.loadText("destination", this.LangId) +" "+count.ToString()+ "</b></div>");
                        loading_unloading.Append("<div id='loading' class='looading'>");
                        loading_unloading.Append("<div style='width:100%;text-transform:uppercase;'>");
                        loading_unloading.Append("<span>Loading:</span>");
                        loading_unloading.Append("<span id='edit_loading_0_" + count.ToString() + "' style='display:none;'>" + dr["loading_id"].ToString() + "</span>");
                        loading_unloading.Append("</div>");
                        loading_unloading.Append("<br>");
                        //Date
                        DateTime _date_var;
                        string date = "N/A";
                        if (DateTime.TryParse(dr["loading_date"].ToString(), out _date_var))
                        {
                            date = _date_var.ToShortDateString();
                        }
                        loading_unloading.Append("<div style='clear:both;'>");
                        loading_unloading.Append("<span>" + DAO.TextBaseFactory.loadText("date", this.LangId) + "</span>");
                        loading_unloading.Append("</div>");
                        loading_unloading.Append("<div>");
                        loading_unloading.Append("<span id='edit_loading_1_" + count.ToString() + "' style='font-weight:bold;'>");
                        loading_unloading.Append(date);
                        loading_unloading.Append("</span>");
                        loading_unloading.Append("</div>");
                        //Time
                        loading_unloading.Append("<div style='clear:both;'>");
                        loading_unloading.Append("<span>" + DAO.TextBaseFactory.loadText("time", this.LangId) + "</span>");
                        loading_unloading.Append("</div>");
                        loading_unloading.Append("<div>");
                        string time_loading = "";
                        string time_ampm = "AM";
                        if (dr["loading_time"].ToString() != "")
                        {
                            string[] time = dr["loading_time"].ToString().Split(' ');
                            if (time.Length == 2)
                            {
                                time_loading = time[0];
                                time_ampm = time[1];
                            }
                        }
                        loading_unloading.Append("<span style='display:none;' id='edit_loading_2_" + count.ToString() + "'>" + time_loading + "</span>");
                        loading_unloading.Append("<span style='display:none;' id='edit_loading_6_" + count.ToString() + "'>" + time_ampm + "</span>");
                        loading_unloading.Append("<span style='font-weight:bold;'>");
                        loading_unloading.Append(dr["loading_time"].ToString() != "" ? dr["loading_time"].ToString() : "N/A");
                        loading_unloading.Append("</span>");
                        loading_unloading.Append("</div>");
                        //Country
                        loading_unloading.Append("<div style='clear:both;'>");
                        loading_unloading.Append("<span>" + DAO.TextBaseFactory.loadText("country", this.LangId) + "</span>");
                        loading_unloading.Append("</div>");
                        loading_unloading.Append("<div>");
                        string country = "N/A";
                        long _country_id = 0;
                        if (Int64.TryParse(dr["loading_country_id"].ToString(), out _country_id))
                        { 
                            country = LoadCountryById(_country_id, false);
                            string country_short = country;
                            if (country.Length > 14)
                            {
                                country_short = country.Substring(0, 14) + "...";
                            }
                            country = "<img width=\"20\" title='" + country +
                            "' height=\"12\" src=\"images/countries/" + dr["loading_country_code"].ToString() + "-t.gif\" /> - " +
                            dr["loading_country_code"].ToString() + " " + country_short;

                        }

                        loading_unloading.Append("<span style='display:none;' id='edit_loading_3_" + count.ToString() + "'>" + _country_id.ToString() + "</span>");
                        loading_unloading.Append("<span style='font-weight:bold;'>");
                        loading_unloading.Append(country);
                        loading_unloading.Append("</span>");
                        loading_unloading.Append("</div>");
                        //City
                        loading_unloading.Append("<div style='clear:both;'>");
                        loading_unloading.Append("<span>" + DAO.TextBaseFactory.loadText("city", this.LangId) + "</span>");
                        loading_unloading.Append("</div>");
                        loading_unloading.Append("<div>");
                        loading_unloading.Append("<span id='edit_loading_4_" + count.ToString() + "' style='font-weight:bold;'>");
                        loading_unloading.Append(dr["loading_city"].ToString() != "" ? dr["loading_city"].ToString() : "N/A");
                        loading_unloading.Append("</span>");
                        loading_unloading.Append("</div>");
                        //Post Code
                        loading_unloading.Append("<div style='clear:both;'>");
                        loading_unloading.Append("<span>" + DAO.TextBaseFactory.loadText("post_code", this.LangId) + "</span>");
                        loading_unloading.Append("</div>");
                        loading_unloading.Append("<div>");
                        loading_unloading.Append("<span id='edit_loading_5_" + count.ToString() + "' style='font-weight:bold;'>");
                        loading_unloading.Append(dr["loading_postcode"].ToString() != "" ? dr["loading_postcode"].ToString() : "N/A");
                        loading_unloading.Append("</span>");
                        loading_unloading.Append("</div>");

                        if (type_transport_id == 167)
                        {
                            //Airport Loading
                            loading_unloading.Append("<div style='clear:both;'>");
                            loading_unloading.Append("<span>" + DAO.TextBaseFactory.loadText("aiport_loading", this.LangId) + "</span>");
                            loading_unloading.Append("</div>");
                            loading_unloading.Append("<div>");
                            loading_unloading.Append("<span id='edit_loading_6_" + count.ToString() + "' style='font-weight:bold;'>");
                            loading_unloading.Append(dr["air_loading_country_name"].ToString() != "" ? dr["air_loading_country_code"].ToString() + " - " + dr["air_loading_country_name"].ToString() : "N/A");
                            loading_unloading.Append("</span>");
                            loading_unloading.Append("</div>");
                        }

                        if (type_transport_id == 165)
                        {
                            //Seaport Loading
                            loading_unloading.Append("<div style='clear:both;'>");
                            loading_unloading.Append("<span>" + DAO.TextBaseFactory.loadText("seaport_loading", this.LangId) + "</span>");
                            loading_unloading.Append("</div>");
                            loading_unloading.Append("<div>");
                            loading_unloading.Append("<span id='edit_loading_7_" + count.ToString() + "' style='font-weight:bold;'>");
                            loading_unloading.Append(dr["sea_loading_country_name"].ToString() != "" ? dr["sea_loading_country_code"].ToString() + " - " + dr["sea_loading_country_name"].ToString() : "N/A");
                            loading_unloading.Append("</span>");
                            loading_unloading.Append("</div>");   
                        }
                        loading_unloading.Append("</div>");


                        ph_loading_uloading.Controls.Add(new LiteralControl(loading_unloading.ToString()));

                        loading_unloading = new StringBuilder();
                        //UnLoading
                        loading_unloading.Append("<div id='loading' class='unloading'>");
                        loading_unloading.Append("<div style='width:100%;text-transform:uppercase;'>");
                        loading_unloading.Append("<span>Unoading:</span>");
                        loading_unloading.Append("<span id='edit_unloading_0_" + count.ToString() + "' style='display:none;'>" + dr["unloading_id"].ToString() + "</span>");
                        loading_unloading.Append("</div>");
                        loading_unloading.Append("<br>");
                        //Date
                        date = "N/A";
                        if (DateTime.TryParse(dr["unloading_date"].ToString(), out _date_var))
                        {
                            date = _date_var.ToShortDateString();
                        }
                        loading_unloading.Append("<div style='clear:both;'>");
                        loading_unloading.Append("<span>" + DAO.TextBaseFactory.loadText("date", this.LangId) + "</span>");
                        loading_unloading.Append("</div>");
                        loading_unloading.Append("<div>");
                        loading_unloading.Append("<span id='edit_unloading_1_" + count.ToString() + "' style='font-weight:bold;'>");
                        loading_unloading.Append(date);
                        loading_unloading.Append("</span>");
                        loading_unloading.Append("</div>");
                        //Time
                        loading_unloading.Append("<div style='clear:both;'>");
                        loading_unloading.Append("<span>" + DAO.TextBaseFactory.loadText("time", this.LangId) + "</span>");
                        loading_unloading.Append("</div>");
                        loading_unloading.Append("<div>");
                        string time_unloading = "";
                        time_ampm = "AM";
                        if (dr["unloading_time"].ToString() != "")
                        {
                            string[] time = dr["unloading_time"].ToString().Split(' ');
                            if (time.Length == 2)
                            {
                                time_unloading = time[0];
                                time_ampm = time[1];
                            }
                        }
                        loading_unloading.Append("<span style='display:none;' id='edit_unloading_2_" + count.ToString() + "'>" + time_unloading + "</span>");
                        loading_unloading.Append("<span style='display:none;' id='edit_unloading_6_" + count.ToString() + "'>" + time_ampm + "</span>");
                        loading_unloading.Append("<span style='font-weight:bold;'>");
                        loading_unloading.Append(dr["loading_time"].ToString() != "" ? dr["unloading_time"].ToString() : "N/A");
                        loading_unloading.Append("</span>");
                        loading_unloading.Append("</div>");
                        //Country
                        loading_unloading.Append("<div style='clear:both;'>");
                        loading_unloading.Append("<span>" + DAO.TextBaseFactory.loadText("country", this.LangId) + "</span>");
                        loading_unloading.Append("</div>");
                        loading_unloading.Append("<div>");
                        country = "N/A";
                        long _uncountry_id = 0;
                        if (Int64.TryParse(dr["unloading_country_id"].ToString(), out _uncountry_id))
                        {
                            country = LoadCountryById(_uncountry_id, false);
                            string country_short = country;
                            if (country.Length > 14)
                            {
                                country_short = country.Substring(0, 14) + "...";
                            }
                            country = "<img width=\"20\" title='" + country +
                            "' height=\"12\" src=\"images/countries/" + dr["unloading_country_code"].ToString() + "-t.gif\" /> - " +
                            dr["unloading_country_code"].ToString() + " " + country_short;
                        }
                        loading_unloading.Append("<span style='display:none;' id='edit_unloading_3_" + count.ToString() + "'>" + _uncountry_id.ToString() + "</span>");
                        loading_unloading.Append("<span style='font-weight:bold;'>");
                        loading_unloading.Append(country);
                        loading_unloading.Append("</span>");
                        loading_unloading.Append("</div>");
                        //City
                        loading_unloading.Append("<div style='clear:both;'>");
                        loading_unloading.Append("<span>" + DAO.TextBaseFactory.loadText("city", this.LangId) + "</span>");
                        loading_unloading.Append("</div>");
                        loading_unloading.Append("<div>");
                        loading_unloading.Append("<span id='edit_unloading_4_" + count.ToString() + "' style='font-weight:bold;'>");
                        loading_unloading.Append(dr["unloading_city"].ToString() != "" ? dr["unloading_city"].ToString() : "N/A");
                        loading_unloading.Append("</span>");
                        loading_unloading.Append("</div>");
                        //Post Code
                        loading_unloading.Append("<div style='clear:both;'>");
                        loading_unloading.Append("<span>" + DAO.TextBaseFactory.loadText("post_code", this.LangId) + "</span>");
                        loading_unloading.Append("</div>");
                        loading_unloading.Append("<div>");
                        loading_unloading.Append("<span id='edit_unloading_5_" + count.ToString() + "' style='font-weight:bold;'>");
                        loading_unloading.Append(dr["unloading_postcode"].ToString() != "" ? dr["unloading_postcode"].ToString() : "N/A");
                        loading_unloading.Append("</span>");
                        loading_unloading.Append("</div>");

                        if (type_transport_id == 167)
                        {
                            //Airport UnLoading
                            loading_unloading.Append("<div style='clear:both;'>");
                            loading_unloading.Append("<span>" + DAO.TextBaseFactory.loadText("aiport_unloading", this.LangId) + "</span>");
                            loading_unloading.Append("</div>");
                            loading_unloading.Append("<div>");
                            loading_unloading.Append("<span id='edit_loading_6_" + count.ToString() + "' style='font-weight:bold;'>");
                            loading_unloading.Append(dr["air_unloading_country_name"].ToString() != "" ? dr["air_unloading_country_code"].ToString() + " - " + dr["air_unloading_country_name"].ToString() : "N/A");
                            loading_unloading.Append("</span>");
                            loading_unloading.Append("</div>");
                        }

                        if (type_transport_id == 165)
                        {
                            //Seaport UnLoading
                            loading_unloading.Append("<div style='clear:both;'>");
                            loading_unloading.Append("<span>" + DAO.TextBaseFactory.loadText("seaport_unloading", this.LangId) + "</span>");
                            loading_unloading.Append("</div>");
                            loading_unloading.Append("<div>");
                            loading_unloading.Append("<span id='edit_loading_7_" + count.ToString() + "' style='font-weight:bold;'>");
                            loading_unloading.Append(dr["sea_unloading_country_name"].ToString() != "" ? dr["sea_unloading_country_code"].ToString() + " - " + dr["sea_unloading_country_name"].ToString() : "N/A");
                            loading_unloading.Append("</span>");
                            loading_unloading.Append("</div>");
                            
                        }
                        loading_unloading.Append("</div>");

                        if (edit_app)
                        {
                            loading_unloading.Append("<div style='clear:both; text-align:right;width:700px;padding-right:30px;'>");
                            loading_unloading.Append("<input type='button' onclick='LoadEditApp(" + count.ToString() + ")' class='btn_add_more'  value='" + DAO.TextBaseFactory.loadText("edit", this.LangId) + "'>");
                            loading_unloading.Append("</div>");
                        }

                        ph_loading_uloading.Controls.Add(new LiteralControl(loading_unloading.ToString()));
                    }
                }
            }
        }
    }
    #endregion

    #region LoadTecnicalInfo
    private void LoadTecnicalInfo(long app_id,long type_transport_id, long type_offer_id)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SP_GetTechnicalInfo", conn))
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Add("@app_id", DbType.Int64).Value = app_id;
                cmd.Parameters.Add("@type_transport_id", DbType.Int64).Value = type_transport_id;
                cmd.Parameters.Add("@type_offer_id", DbType.Int64).Value = type_offer_id;
                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    long level_importance_id;
                    lblLevelOfImportance.Text = "N/A";
                    if (dr["level_importance_id"] != null)
                    {
                        if (long.TryParse(dr["level_importance_id"].ToString(), out level_importance_id))
                        {
                            lblLevelOfImportance.Text = LoadUniversalType(level_importance_id);
                            if (ddlLevelOfImportnce.Items.FindByValue(dr["level_importance_id"].ToString()) != null)
                            {
                                ddlLevelOfImportnce.Items.FindByValue(dr["level_importance_id"].ToString()).Selected = true;
                            }
                        }
                    }
                    long universal_type_id;
                    lblVehicleType.Text = "N/A";
                    if (long.TryParse(dr["universal_type_id"].ToString(), out universal_type_id))
                    {
                        lblVehicleType.Text = LoadUniversalType(universal_type_id);
                        if (ddlVehicleTypes.Items.FindByValue(dr["universal_type_id"].ToString()) != null)
                        {
                            ddlVehicleTypes.Items.FindByValue(dr["universal_type_id"].ToString()).Selected = true;
                        }
                    }
                    long placement_type_id;
                    lblPlacementType.Text = "N/A";
                    if (long.TryParse(dr["placement_type_id"].ToString(), out placement_type_id))
                    {
                        lblPlacementType.Text = LoadUniversalType(placement_type_id);
                        if (ddlPlacementTypes.Items.FindByValue(dr["placement_type_id"].ToString()) != null)
                        {
                            ddlPlacementTypes.Items.FindByValue(dr["placement_type_id"].ToString()).Selected = true;
                        }
                    }
                    long packing_type_id;
                    lblPackingType.Text = "N/A";
                    if (long.TryParse(dr["packing_type_id"].ToString(), out packing_type_id))
                    {
                        lblPackingType.Text = LoadUniversalType(packing_type_id);
                        if (ddlPackingTypes.Items.FindByValue(dr["packing_type_id"].ToString()) != null)
                        {
                            ddlPackingTypes.Items.FindByValue(dr["packing_type_id"].ToString()).Selected = true;
                        }
                    }
                    long special_conditions_id;
                    lblSpecCondition.Text = "N/A";
                    if (long.TryParse(dr["special_conditions_id"].ToString(), out special_conditions_id))
                    {
                        lblSpecCondition.Text = LoadUniversalType(special_conditions_id);
                        if (ddlSpecualConditionTypes.Items.FindByValue(dr["special_conditions_id"].ToString()) != null)
                        {
                            ddlSpecualConditionTypes.Items.FindByValue(dr["special_conditions_id"].ToString()).Selected = true;
                        }
                    }

                    lblVolume.Text = dr["volume"].ToString() != "" ? dr["volume"].ToString() : "N/A"; ;
                    if (dr["volume"].ToString() != "")
                    {
                        string[] volume = dr["volume"].ToString().Split(' ');
                        if (volume.Length == 2)
                        {
                            if (ddlVolume.Items.FindByValue(volume[1]) != null)
                            {
                                ddlVolume.Items.FindByValue(volume[1]).Selected = true;
                            }
                            tbVolume.Text = volume[0];
                        }
                    }
                    
                    lblPrice.Text = dr["price"].ToString() != "" ? dr["price"].ToString() : "N/A"; ;
                    if (dr["price"].ToString() != "")
                    {
                        string[] price = dr["price"].ToString().Split(' ');
                        if (price.Length == 2)
                        {
                            if (ddlPrice.Items.FindByValue(price[1]) != null)
                            {
                                ddlPrice.Items.FindByValue(price[1]).Selected = true;
                            }
                            tbPrice.Text = price[0];
                        }
                    }
                    
                    lblWidth.Text = dr["width"].ToString() != "" ? dr["width"].ToString() : "N/A"; ;
                    if (dr["width"].ToString() != "")
                    {
                        string[] width = dr["width"].ToString().Split(' ');
                        if (width.Length == 2)
                        {
                            if (ddlWidth.Items.FindByValue(width[1]) != null)
                            {
                                ddlWidth.Items.FindByValue(width[1]).Selected = true;
                            }
                            tbWidth.Text = width[0];
                        }
                    }
                    
                    lblLenght.Text = dr["length"].ToString() != "" ? dr["length"].ToString() : "N/A"; ;
                    if (dr["length"].ToString() != "")
                    {
                        string[] length = dr["length"].ToString().Split(' ');
                        if (length.Length == 2)
                        {
                            if (ddlLenght.Items.FindByValue(length[1]) != null)
                            {
                                ddlLenght.Items.FindByValue(length[1]).Selected = true;
                            }
                            tbLenght.Text = length[0];
                        }
                    }
                    
                    lblHeight.Text = dr["height"].ToString() != "" ? dr["height"].ToString() : "N/A";
                    if (dr["height"].ToString() != "")
                    {
                        string[] height = dr["height"].ToString().Split(' ');
                        if (height.Length == 2)
                        {
                            if (ddlHeight.Items.FindByValue(height[1]) != null)
                            {
                                ddlHeight.Items.FindByValue(height[1]).Selected = true;
                            }
                            tbHeight.Text = height[0];
                        }
                    }

                    lblWeight.Text = dr["weight"].ToString() != "" ? dr["weight"].ToString() : "N/A";
                    if (dr["weight"].ToString() != "")
                    {
                        string[] weight = dr["weight"].ToString().Split(' ');
                        if (weight.Length == 2)
                        {
                            if (ddlWeight.Items.FindByValue(weight[1]) != null)
                            {
                                ddlWeight.Items.FindByValue(weight[1]).Selected = true;
                            }
                            tbWeight.Text = weight[0];
                        }
                    }
                    
                    lblOtherInfo.Text = dr["other_information"].ToString() != "" ? dr["other_information"].ToString() : "N/A";
                    tbOtherInformattions.Text = lblOtherInfo.Text;
                }
                dr.Close();
            }
        }
    }
    #endregion

    #region LoadCountryById
    private string LoadCountryById(long id,bool short_name)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT id, short, name FROM dbo.Fn_GetPlace(@id)", conn))
            {
                cmd.Parameters.Add("id", id);
                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    if (short_name)
                        return dr["short"].ToString();
                    else
                        return dr["name"].ToString();
                }
                dr.Close();
                return "";
            }
        }
    }
    #endregion

    #region LoadUniversalType
    private string LoadUniversalType(long id)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            string cmd1 = "";
            Int64 langId = DAO.Connection.DEFAULT_LANGUAGE;
            if (Session["language"] != null)
            {
                Int64.TryParse(Session["language"].ToString(), out langId);
            }
            if (langId != DAO.Connection.DEFAULT_LANGUAGE)
                cmd1 = "SELECT id, name FROM dbo.Fn_GetUniversalTypeByLang(@id,@language_id)";
            else
                cmd1 = "SELECT id, name FROM dbo.Fn_GetUniversalTypeById(@id,@language_id)";
            using (SqlCommand cmd = new SqlCommand(cmd1, conn))
            {
                cmd.Parameters.Add("id", id);
                cmd.Parameters.Add("language_id", langId);

                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    return dr["name"].ToString();
                }
                dr.Close();
                return "";
            }
        }
    }
    #endregion

    #region LoadDimensionTypes
    private void LoadDimensionTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc')", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 18);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                
                ddlLenght.Items.Clear();
                ddlWidth.Items.Clear();
                ddlHeight.Items.Clear();
               
                while (dr.Read())
                {
                    ListItem li = new ListItem(dr["name"].ToString(), dr["name"].ToString());
                    ddlHeight.Items.Add(li);
                    ddlLenght.Items.Add(li);
                    ddlWidth.Items.Add(li);
                    
                }
                dr.Close();
            }
        }
    }
    #endregion

    #region LoadVolumeTypes
    private void LoadVolumeTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc')", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 19);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlVolume.Items.Clear();

                while (dr.Read())
                {
                    ddlVolume.Items.Add(new ListItem(dr["name"].ToString(), dr["name"].ToString()));
                }
                dr.Close();
            }
        }
    }
    #endregion

    #region LoadCurrencyTypes
    private void LoadCurrencyTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc')", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 11);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlPrice.Items.Clear();
                while (dr.Read())
                {
                    ddlPrice.Items.Add(new ListItem(dr["name"].ToString(), dr["name"].ToString()));
                }
                dr.Close();
            }
        }
    }
    #endregion

    #region LoadWeightTypes
    private void LoadWeightTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc')", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 20);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlWeight.Items.Clear();
                while (dr.Read())
                {
                    ddlWeight.Items.Add(new ListItem(dr["name"].ToString(), dr["name"].ToString()));
                }
                dr.Close();
            }
        }
    }
    #endregion

    #region LoadVehicleTypes
    private void LoadVehicleTypes(Int64 langId, Int64 type_id)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,10000,'id','desc') WHERE isActive='true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", type_id);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlVehicleTypes.Items.Clear();
                ddlVehicleTypes.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    ddlVehicleTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                }
                dr.Close();
            }
        }
    }
    #endregion

    #region LoadLevelOfImportanceTypes
    private void LoadLevelOfImportanceTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc')", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 16);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlLevelOfImportnce.Items.Clear();
                ddlLevelOfImportnce.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    ddlLevelOfImportnce.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                }
                dr.Close();
            }
        }
    }
    #endregion

    #region LoadPlacementTypes
    private void LoadPlacementTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc')", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 8);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlPlacementTypes.Items.Clear();
                ddlPlacementTypes.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    ddlPlacementTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                }
                dr.Close();
            }
        }
    }
    #endregion

    #region LoadPackingTypes
    private void LoadPackingTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc')", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 6);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlPackingTypes.Items.Clear();
                ddlPackingTypes.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    ddlPackingTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                }
                dr.Close();
            }
        }
    }
    #endregion

    #region LoadSpecialCondTypes
    private void LoadSpecialCondTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc')", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 5);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlSpecualConditionTypes.Items.Clear();
                ddlSpecualConditionTypes.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    ddlSpecualConditionTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                }
                dr.Close();
            }
        }
    }
    #endregion

    #region InitializeUI
    protected override void InitializeUI()
    {
        long langId = DAO.Connection.DEFAULT_LANGUAGE;
        if (Session["language"] != null)
            long.TryParse(Session["language"].ToString(), out langId);

        this.LangId = Convert.ToInt32(langId);
    }
    #endregion

    #region btn_Save_to_Favorites_Click
    protected void btn_Save_to_Favorites_Click(object sender, EventArgs e)
    {
        long cid = 0;
        if (Request.Cookies["texchange"]["cid"] != null && DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["cid"].ToString()) != "")
        {
            Int64.TryParse(DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["cid"].ToString()), out cid);
        }

        int appid = 0;
        if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
        {  
            Int32.TryParse(Request.QueryString["id"].ToString(), out appid);
        }

        if(cid>0 && appid>0)
        {
            using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT count(id) FROM Favorites_App WHERE company_id = @company_id AND app_id = @app_id", conn))
                {
                    cmd.Parameters.Add("company_id", cid);
                    cmd.Parameters.Add("app_id", appid);
                    int record_count = (int)cmd.ExecuteScalar();

                    if (record_count == 0)
                    {
                        using (SqlCommand cmd1 = new SqlCommand("INSERT INTO Favorites_App VALUES(@company_id, @app_id)", conn))
                        {
                            cmd1.Parameters.Add("company_id", cid);
                            cmd1.Parameters.Add("app_id", appid);
                            object val = null;
                            try
                            {
                                val = cmd1.ExecuteScalar();
                            }
                            catch (Exception ex)
                            {
                                Response.Write(ex.Message);
                            }
                        }
                    }
                }
            }
        }
    }
    #endregion

    #region btn_Update_App_Click
    protected void btn_Update_App_Click(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SP_UpdLoadingUnloading", conn))
            {
                long loading_id = 0;
                if (Int64.TryParse(tbLoadingID.Text, out loading_id) && loading_id > 0)
                {
                    //Loading
                    SqlParameter[] pars = new SqlParameter[8];
                    pars[0] = new SqlParameter("country_id", ddlCountryLoading.SelectedValue);
                    pars[0].DbType = System.Data.DbType.Int64;
                    pars[1] = new SqlParameter("city", tbCityLoading.Text);
                    pars[1].DbType = System.Data.DbType.String;
                    pars[2] = new SqlParameter("postcode", tbPostCodeLoading.Text);
                    pars[2].DbType = System.Data.DbType.String;
                    pars[3] = new SqlParameter("time", tbTimeLoading.Text + " " + ddlAMPMLoading.SelectedValue);
                    pars[3].DbType = System.Data.DbType.String;
                    DateTime loading_date;
                    if (DateTime.TryParse(tbDateLoading.Text, out loading_date))
                    {
                        pars[4] = new SqlParameter("date", loading_date);
                        pars[4].DbType = System.Data.DbType.DateTime;
                    }
                    pars[5] = new SqlParameter("id", loading_id);
                    pars[5].DbType = System.Data.DbType.Int64;
                    long air_id = 0;
                    long sea_id = 0;
                    if (tb_type_transport_id.Text.Trim() == "167" && ddlAirportLoading.Visible)
                    {
                        Int64.TryParse(ddlAirportLoading.SelectedValue, out air_id);
                    }
                    if (tb_type_transport_id.Text.Trim() == "165" && ddlSeaPortLoading.Visible)
                    {
                        Int64.TryParse(ddlSeaPortLoading.SelectedValue, out sea_id);
                    }
                    pars[6] = new SqlParameter("airport_id", air_id);
                    pars[6].DbType = System.Data.DbType.Int64;
                    pars[7] = new SqlParameter("seaport_id", sea_id);
                    pars[7].DbType = System.Data.DbType.Int64;

                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    foreach (SqlParameter p in pars)
                    {
                        if (p != null && p.Value != null)
                        {
                            if (p.Value.ToString() != "0")
                                cmd.Parameters.Add(p);
                        }
                    }
                    cmd.ExecuteScalar();
                }

                long unloading_id = 0;
                if (Int64.TryParse(tbUnloadingID.Text, out unloading_id) && unloading_id > 0)
                {
                    //Unloading
                    SqlParameter[] pars = new SqlParameter[8];
                    pars[0] = new SqlParameter("country_id", ddlCountryUnloading.Text);
                    pars[0].DbType = System.Data.DbType.Int64;
                    pars[1] = new SqlParameter("city", tbCityUnloading.Text);
                    pars[1].DbType = System.Data.DbType.String;
                    pars[2] = new SqlParameter("postcode", tbPostCodeUnloading.Text);
                    pars[2].DbType = System.Data.DbType.String;
                    pars[3] = new SqlParameter("time", tbTimeUnloading.Text + " " + ddlAMPMUnloading.SelectedValue);
                    pars[3].DbType = System.Data.DbType.String;
                    DateTime unloading_date;
                    if (DateTime.TryParse(tbDateUnloading.Text, out unloading_date))
                    {
                        pars[4] = new SqlParameter("date", unloading_date);
                        pars[4].DbType = System.Data.DbType.DateTime;
                    }
                    pars[5] = new SqlParameter("id", unloading_id);
                    pars[5].DbType = System.Data.DbType.Int64;

                    long air_id = 0;
                    long sea_id = 0;
                    if (type_transs_id == "167" && ddlAirportUnloading.Visible)
                    {
                        Int64.TryParse(ddlAirportUnloading.SelectedValue, out air_id);
                    }
                    if (type_transs_id == "165" && ddlSeaPortUnloading.Visible)
                    {
                        Int64.TryParse(ddlSeaPortUnloading.SelectedValue, out sea_id);
                    }
                    pars[6] = new SqlParameter("airport_id", air_id);
                    pars[6].DbType = System.Data.DbType.Int64;
                    pars[7] = new SqlParameter("seaport_id", sea_id);
                    pars[7].DbType = System.Data.DbType.Int64;

                    cmd.Parameters.Clear();
                    foreach (SqlParameter p in pars)
                    {
                        if (p != null && p.Value != null)
                        {
                            if (p.Value.ToString() != "0")
                                cmd.Parameters.Add(p);
                        }
                    }
                    cmd.ExecuteScalar();
                }
            }

            if (Request.Cookies["texchange"] != null && Request.Cookies["texchange"]["logged"] != null && 
                DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["logged"]) == "true" && Request.Cookies["texchange"]["type"] != null && 
                DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["type"].ToString()) == "admin")
            {
                long app_id = 0;
                if (Int64.TryParse(tb_appid.Text, out app_id) && app_id > 0)
                {
                    using (SqlCommand cmd = new SqlCommand("SP_UpdateApplication", conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        SqlParameter[] pars = new SqlParameter[2];
                        pars[0] = new SqlParameter("id", app_id);
                        pars[0].DbType = System.Data.DbType.Int64;
                        pars[1] = new SqlParameter("isActive", cb_isActive.Checked);
                        pars[1].DbType = System.Data.DbType.Boolean;
                        cmd.Parameters.Clear();
                        foreach (SqlParameter p in pars)
                        {
                            if (p != null)
                            {
                                cmd.Parameters.Add(p);
                            }
                        }
                        cmd.ExecuteScalar();
                    }
                }
                Response.Redirect("~/Application.aspx");
            }
            else
            {
                Response.Redirect("~/MyApplications.aspx");
            }
        }
    }
    #endregion

    #region btn_UpdateTechicalInfo_Click
    protected void btn_UpdateTechicalInfo_Click(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            long app_id = 0;
            if (Int64.TryParse(tb_appid.Text, out app_id) && app_id > 0)
            {
                using (SqlCommand cmd = new SqlCommand("SP_UpdateTechnicalInfo", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] pars = new SqlParameter[15];
                    pars[0] = new SqlParameter("app_id", app_id);
                    pars[0].DbType = System.Data.DbType.Int64;
                    pars[1] = new SqlParameter("level_importance_id", ddlLevelOfImportnce.SelectedValue);
                    pars[1].DbType = System.Data.DbType.Int64;
                    pars[2] = new SqlParameter("universal_type_id", ddlVehicleTypes.SelectedValue);
                    pars[2].DbType = System.Data.DbType.Int64;
                    pars[3] = new SqlParameter("placement_type_id", ddlPlacementTypes.SelectedValue);
                    pars[3].DbType = System.Data.DbType.Int64;
                    pars[4] = new SqlParameter("packing_type_id", ddlPackingTypes.SelectedValue);
                    pars[4].DbType = System.Data.DbType.Int64;
                    pars[5] = new SqlParameter("length", tbLenght.Text + " " + ddlLenght.SelectedValue);
                    pars[5].DbType = System.Data.DbType.String;
                    pars[6] = new SqlParameter("width", tbWidth.Text + " " + ddlWidth.SelectedValue);
                    pars[6].DbType = System.Data.DbType.String;
                    pars[7] = new SqlParameter("height", tbHeight.Text + " " + ddlHeight.SelectedValue);
                    pars[7].DbType = System.Data.DbType.String;
                    pars[8] = new SqlParameter("volume", tbVolume.Text + " " + ddlVolume.SelectedValue);
                    pars[8].DbType = System.Data.DbType.String;
                    pars[9] = new SqlParameter("price", tbPrice.Text + " " + ddlPrice.SelectedValue);
                    pars[9].DbType = System.Data.DbType.String;
                    pars[10] = new SqlParameter("special_conditions_id", ddlSpecualConditionTypes.SelectedValue);
                    pars[10].DbType = System.Data.DbType.Int64;
                    pars[11] = new SqlParameter("type_offer_id", Int64.Parse(tb_type_offer_id.Text));
                    pars[11].DbType = System.Data.DbType.Int64;
                    pars[12] = new SqlParameter("type_transport_id", Int64.Parse(tb_type_transport_id.Text));
                    pars[12].DbType = System.Data.DbType.Int64;
                    pars[13] = new SqlParameter("other_information", tbOtherInformattions.Text);
                    pars[13].DbType = System.Data.DbType.String;
                    pars[14] = new SqlParameter("weight", tbWeight.Text + " " + ddlWeight.SelectedValue);
                    pars[14].DbType = System.Data.DbType.String;
                    cmd.Parameters.Clear();
                    foreach (SqlParameter p in pars)
                    {
                        if (p != null)
                        {
                            cmd.Parameters.Add(p);
                        }
                    }
                    cmd.ExecuteScalar();
                }
            }
        }

        if (Request.Cookies["texchange"] != null && Request.Cookies["texchange"]["logged"] != null && 
            DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["logged"]) == "true" && Request.Cookies["texchange"]["type"] != null && 
            DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["type"].ToString()) == "admin")
        {
            Response.Redirect("~/Application.aspx");
        }
        else
        {
            Response.Redirect("~/MyApplications.aspx");
        }
    }
    #endregion

    protected void btn_back_to_previous_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/application.aspx");
    }

    protected void btn_Delete_App_Click(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            long app_id = 0;
            long type_trans_id = 0;
            long type_offer_id = 0;
            if (Int64.TryParse(tb_appid.Text, out app_id) && app_id > 0 &&
                Int64.TryParse(tb_type_offer_id.Text, out type_offer_id) && 
                Int64.TryParse(tb_type_transport_id.Text, out type_trans_id))
            {
                using (SqlCommand cmd = new SqlCommand("SP_DeleteApplication", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] pars = new SqlParameter[3];
                    pars[0] = new SqlParameter("app_id", app_id);
                    pars[0].DbType = System.Data.DbType.Int64;
                    pars[1] = new SqlParameter("type_transport_id", type_trans_id);
                    pars[1].DbType = System.Data.DbType.Int64;
                    pars[2] = new SqlParameter("type_offer_id", type_offer_id);
                    pars[2].DbType = System.Data.DbType.Int64;
                    cmd.Parameters.Clear();
                    foreach (SqlParameter p in pars)
                    {
                        if (p != null)
                        {
                            cmd.Parameters.Add(p);
                        }
                    }
                    cmd.ExecuteScalar();
                    if (Request.Cookies["texchange"] != null && Request.Cookies["texchange"]["logged"] != null &&
                    DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["logged"]) == "true" && Request.Cookies["texchange"]["type"] != null &&
                    DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["type"].ToString()) == "admin")
                    {
                        Response.Redirect("~/Application.aspx");
                    }
                    else
                    {
                        Response.Redirect("~/MyApplications.aspx");
                    }
                }
            }
        }
    }
}
